#include "task.h"

unsigned char cntr_temp = 0;
unsigned char cntr_soc = 0;
unsigned char cntr_lig = 0;
unsigned char cntr_key = 0;
unsigned char cntr_dsp = 0;
unsigned char cntr_led = 0;

unsigned int distance = 0;				//距离
unsigned int temp = 0;					//温度
unsigned char light = 0;				//光照度
unsigned int DAC_out = 0;				//DAC输出，放大10倍
bit mode = 0;
bit ui = 0;

//1ms中断一次@12MHz
void initSysTick(void)
{
    AUXR |= 0x04;
    T2L = 0x20;
	T2H = 0xD1;
	AUXR |= 0x10;
    IE2 |= 0x04;
	EA = 1;
}

//初始化板级外设
void initBsp(void)
{
	BUZ(0x00);
	LED(0xFF);
	InitDsp();
	InitKeyStruct();
	InitPCF8591();
}

//定时器中断服务函数
void isrSysTick(void)    interrupt 12
{
	GetDisplayStruct()->SegDisplay();
	
	if(cntr_temp < 200){
		++cntr_temp;
	}
	
	if(cntr_lig < 150){
		++cntr_lig;
	}

	if(cntr_led < 100){
		++cntr_led;
	}
	
	if(cntr_dsp < 50){
		++cntr_dsp;
	}
		
	if(cntr_key < 10){
		++cntr_key;
	}
}

//
void taskControl(void)
{
	static unsigned char led = 0xff;
	static unsigned int DAC = 0;
		
	//100ms 刷新
	if(cntr_led < 100)
		return;
	cntr_led = 0;
	
	if(mode == 0){
		led &= ~1;
		led |= 2;
	}else if(mode == 1){
		led &= ~2;
		led |= 1;
	}else{
		led |= 1;
		led |= 2;
	}
	
	if(mode == 0){
		if(temp/10 < 10){
			DAC_out = 10;
		}else if(temp/10 > 40){
			DAC_out = 50;
		}else{
			/* y=(2x-5)/15*/
			DAC_out = (2*temp/10 - 5)*10/15;
		}	 
	}else{
		if(light < 10){
			DAC_out = 10;
		}else if(light > 240){
			DAC_out = 50;
		}else{
			/* y=(2x+95)/115*/
			DAC_out = (2*light + 95)*10/115;
		}
	}
	DAC = (unsigned int)(DAC_out*5.1);
	SetDAC(DAC);
	LED(led);
}


//
void taskCollect(void)
{
	/* 温度数据 */
	if(cntr_temp == 200){
		cntr_temp = 0;
		temp = (unsigned int)(getTemperature() * 10);
	}		
	
	/* 光照度数据 */
	if(cntr_lig == 150){
		cntr_lig = 0;
		light = GetADC(1);
	}
	
    if(cntr_key == 10){
		cntr_key = 0;
        GetKeyStruct()->pFun();
    }		
}

//
void taskKey(void)
{
	//按键部分处理
	switch(GetKeyStruct()->value){
		/* S4 */
		case 1:
			if(ui == 0){
				mode = ~mode;
			}
			GetKeyStruct()->value = 255;
			break;
		/* S5 */
		case 3:
			ui = ~ui;
			GetKeyStruct()->value = 255;
			break;
		default:
			GetKeyStruct()->value = 255;			
			break;
	}
}
	

//显示处理
void taskDisplay(void)
{
	DisplayStruct* seg = GetDisplayStruct();
	
	//显示部分处理
	if(cntr_dsp < 50){
		return;
	}	
	cntr_dsp = 0;
	
	/* 模式界面 */
	if(ui == 0){
		if(mode == 0){
			/* 温度控制模式 */
			seg->dot = 6;
			seg->buf[0] = 1;
			seg->buf[1] = 10;
			seg->buf[2] = 10;
			seg->buf[3] = 10;
			seg->buf[4] = 10;
			seg->buf[5] = temp/100;
			seg->buf[6] = temp%100/10;
			seg->buf[7] = temp%10;
		}else{
			/* 光照度控制模式 */
			seg->dot = 255;
			seg->buf[0] = 2;
			seg->buf[1] = 10;
			seg->buf[2] = 10;
			seg->buf[3] = 10;
			seg->buf[4] = 10;
			seg->buf[5] = (light > 99)?(light / 100):10;
			seg->buf[6] = (light > 9)?(light % 100/10):10;
			seg->buf[7] = light%10;	
		}			
	}else{
	/* 输出界面 */
		seg->dot = 6;
		seg->buf[0] = 13;
		seg->buf[1] = 10;
		seg->buf[2] = 10;
		seg->buf[3] = 10;
		seg->buf[4] = 10;
		seg->buf[5] = 10;
		seg->buf[6] = DAC_out/10;
		seg->buf[7] = DAC_out%10;	
	}	
}